package com.loubobooo.personal.blog.controller.backend;

import com.loubobooo.personal.blog.common.ServerResponse;
import com.loubobooo.personal.blog.entity.User;
import com.loubobooo.personal.blog.entity.vo.UserVO;
import com.loubobooo.personal.blog.service.UserService;
import com.loubobooo.personal.blog.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller @RequestMapping("/admin/user") public class UserManageController {

    @Autowired private UserService userService;

    //后台用户列表显示
    @RequestMapping(value = "") public ModelAndView userList() {
        ModelAndView modelandview = new ModelAndView();

        List<UserVO> userVOList = userService.listUser();
        modelandview.addObject("userVOList", userVOList);

        modelandview.setViewName("Admin/User/index");
        return modelandview;

    }

    //后台添加用户页面显示
    @RequestMapping(value = "/insert") public ModelAndView insertUserView() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Admin/User/insert");
        return modelAndView;
    }

    @RequestMapping(value = "/checkUserName", method = RequestMethod.POST) @ResponseBody public ServerResponse checkUserName(
            HttpServletRequest request) {
        String username = request.getParameter("username");
        int id = Integer.valueOf(request.getParameter("id"));

        User user = userService.getUserByNameOrEmail(username, null);
        //用户名已存在,但不是当前用户(编辑用户的时候，不提示)
        if (user != null && user.getUserId() != id) {
            return ServerResponse.createByErrorMessage("用户名已存在!");
        }
        return ServerResponse.createBySuccess();
    }

    @RequestMapping(value = "/checkUserEmail", method = RequestMethod.POST) @ResponseBody public ServerResponse checkUserEmail(
            HttpServletRequest request) {
        String email = request.getParameter("email");
        int id = Integer.valueOf(request.getParameter("id"));

        User user = userService.getUserByNameOrEmail(null, email);
        if(user != null && user.getUserId() != id){
            //用户名已存在,但不是当前用户(编辑用户的时候，不提示)
            return ServerResponse.createByErrorMessage("电子邮箱已存在!");
        }
        return ServerResponse.createBySuccess();
    }

    // 后台添加用户页面提交
    @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) public String insertUserSubmit(User user) {
        if (userService.getUserByNameOrEmail(user.getUserName(), user.getUserEmail()) == null) {
            String md5Password = MD5Util.MD5EncodeUtf8(user.getUserPassword());
            user.setUserPassword(md5Password);
            user.setUserRegisterTime(new Date());
            user.setUserStatus(1);
            userService.insertUser(user);
        }
        return "redirect:/admin/user";
    }

    //删除用户
    @RequestMapping(value = "/delete/{id}") public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
        return "redirect:/admin/user";
    }

    //编辑用户页面显示
    @RequestMapping(value = "/edit/{id}") public ModelAndView editUserView(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();

        UserVO userVO = userService.getUserById(id);
        modelAndView.addObject("userVO", userVO);

        modelAndView.setViewName("Admin/User/edit");
        return modelAndView;
    }

    //编辑用户提交
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) public String editUserSubmit(User user) {
        if (user != null && StringUtils.isNoneBlank(user.getUserPassword())) {
            String md5Password = MD5Util.MD5EncodeUtf8(user.getUserPassword());
            user.setUserPassword(md5Password);
            userService.updateUser(user);
        }
        return "redirect:/admin/user";
    }

    //基本信息页面显示
    @RequestMapping(value = "/profile/{id}") public ModelAndView userProfileView(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();

        UserVO userVO = userService.getUserById(id);
        modelAndView.addObject("userVO", userVO);

        modelAndView.setViewName("Admin/User/profile");
        return modelAndView;
    }
}
